﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.ComponentModel;

namespace Test_ProDivingJudge2salmon14
{
    public static class DataManager
    {
        #region Public methods
        public static int judgeID { get; set; }
        public static string hostAdress { get; set; }
        public static string send { get; set; }
        public static string received { get; set; }
        #endregion

        #region Private methods
        private static byte[] buffer = new byte[1024];
        private static Socket _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        #endregion

        #region Public static methods
        /// <summary>
        /// Method sends the current string, send. To the server.
        /// </summary>
        public static void SendData()
        {
            try
            {
                buffer = Encoding.ASCII.GetBytes(send);
                _clientSocket.Send(buffer);
            }
            catch (SocketException e)
            {
                System.Console.WriteLine(e.Message);
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                LoopConnect();
            }
        }

        /// <summary>
        /// Method that sits and waits for data from the server.
        /// Will only return when it receives something.
        /// </summary>
        public static void ReceiveData()
        {
            try
            {
                received = "";
                byte[] receivedBuff = new byte[1024];
                int rec = 0;
                while (!received.Contains('!'))
                {
                    rec = _clientSocket.Receive(receivedBuff);

                    if (rec > 0)
                    {
                        byte[] data = new byte[rec];
                        Array.Copy(receivedBuff, data, rec);

                        received = Encoding.ASCII.GetString(data);
                    }
                }
                received = received.Trim('!');
                System.Console.WriteLine("Client text received: " + received);
            }
            catch (SocketException e)
            {
                System.Console.WriteLine(e.Message);
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                LoopConnect();
            }
        }

        /// <summary>
        /// A loop that tries to connect to a given severs local IP.
        /// </summary>
        public static void LoopConnect()
        {
            int attempts = 0;
            while (!_clientSocket.Connected)
            {
                try
                {
                    attempts++;
                    _clientSocket.Connect(IPAddress.Parse("192.168.33.2"), 100);
                }
                catch (SocketException)
                {
                    System.Console.WriteLine("Attempts: " + attempts);
                }
            }
        } 
        #endregion
    }
}
